/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

RUM_AHel_movewaypoint = {
	private ["_mkrname","_grp","_pos","_wpAH"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_pos = [0,0,0];
	if (count _this == 2) then {
		_pos = getMarkerpos _mkrname;
	} else {
		_pos = getpos (_this select 2);
	};
	_wpAH=_grp addWaypoint [_pos, 0];
	_wpAH setWaypointType "MOVE";
	_wpAH setWaypointSpeed "NORMAL";
	_wpAH setWaypointCombatMode "BLUE";
	_wpAH setWaypointCompletionRadius 50;
	_wpAH setWaypointStatements ["true", ""];
};

RUM_AHel_MoveWP = {
	private ["_mkrname","_grp","_wpNum","_pos","_helo"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_helo = _this select 2;
	_pos = [0,0,0];
	_pos = getMarkerpos _mkrname;
	_grp setCombatMode "RED";
	_grp setBehaviour "COMBAT";
	_grp setSpeedMode "FULL";
	_helo flyInHeight RUM_AHEL_flyHeight;
	_helo doMove _pos;

	while {_helo distance _pos > 225 && canMove _helo} do {sleep 1};
	
	player globalchat "after while";
	_dir = ((_pos select 0) - (getpos _helo select 0)) atan2 ((_pos select 1) - (getpos _helo select 1));
	_wp_target = [((_pos select 0) + (20 * sin _dir)),((_pos select 1) + (20 * cos _dir)),0];
	
	//reduced flypast distance
	_helo doMove _wp_target;
	while {_helo distance _pos > 100 && canMove _helo} do {
		_helo doMove _wp_target;
		player globalchat "next while";
		sleep 5;
	};
};

RUM_AHel_SAD = {
	private ["_mkrname","_grp","_pos","_wpAH"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_pos = [0,0,0];
	if (count _this == 2) then {_pos = getMarkerpos _mkrname} else {_pos = getpos (_this select 2)};
	// _grp setCombatMode "RED";
	// _grp setBehaviour "COMBAT";
	// _grp setSpeedMode "FULL";
	// _helo flyInheight 125;

	_wpAH=_grp addWaypoint [_pos, 0];
	_wpAH setWaypointType "SAD";
	_wpAH setWaypointSpeed "FULL";
	_wpAH setWaypointCombatMode "RED";
	_wpAH setWaypointCompletionRadius 300;
	_wpAH setWaypointStatements ["true", ""];
	sleep 120;
};

RUM_AHel_moveWPTakeoff = {
	private ["_mkrname","_grp","_pos","_helo"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_helo = _this select 2;
	_pos = [0,0,0];
	if(count _this == 3)then{_pos = getMarkerpos _mkrname}else{_pos = getpos (_this select 3)};
	_wpAH=_grp addWaypoint [_pos, 0];
	_wpAH setWaypointType "MOVE";
	_wpAH setWaypointCombatMode "GREEN";
	_wpAH setWaypointSpeed "NORMAL";
	_wpAH setWaypointBehaviour "STEALTH";
	_wpAH setWaypointCompletionRadius 50;
	_helo flyInheight 18;
};

RUM_AHel_moveWPNormal = {
	private ["_mkrname","_grp","_wpNum","_pos","_wpAH","_home","_heloTakeOff2","_helo"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_helo = _this select 2;
	_home = _this select 3;
	_pos = [0,0,0];
	if(count _this == 4)then{_pos = getMarkerpos _mkrname}else{_pos = getpos (_this select 4)};
	_heloTakeOff2 = true;			
	while {_heloTakeOff2} do {
		_distanceToDeck = round(getposASL _helo select 2);
		_heightToHomePlate = (_distanceToDeck - (getposASL _home select 2));
		_distToHomePlate = round( _helo distance _home);
		if (_distToHomePlate >= 55) then {
			deleteWaypoint [_grp, 0];
			_wpAH=_grp addWaypoint [_pos, 0];
			_wpAH setWaypointType "MOVE";
			_wpAH setWaypointCombatMode "GREEN";
			_wpAH setWaypointSpeed "NORMAL";
			_wpAH setWaypointBehaviour "STEALTH";
			_wpAH setWaypointCompletionRadius 50;
			_helo flyInheight 45;
			sleep 7;
			_helo flyInheight RUM_AHEL_flyHeight;
			_heloTakeOff2 = false;
		};
	};
};
RUM_AHel_Spawn = {
	private ["_mkrname","_grp","_wpNum","_pos","_wpAH","_stg","_helo"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_wpNum = _this select 2;
	_pos = [0,0,0];
	if(count _this == 3)then{_pos = getMarkerpos _mkrname}else{_pos = getpos (_this select 3)};
	_wpAH=_grp addWaypoint [_pos, 0];
	_wpAH setWaypointType "MOVE";
	_wpAH setWaypointSpeed "FULL";
	_wpAH setWaypointCombatMode "BLUE";
	_wpAH setWaypointCompletionRadius 175;
	_stg = "RUM_AHel_endflight = '"+(format ["%1",_helo])+"'";
	_wpAH setWaypointStatements ["true", _stg];

	/* wait for end of waypoint flight */
	_end = format ["%1",_helo];
	waitUntil{
		sleep 0.15;
		(!alive _helo) OR (RUM_AHel_endflight == _end);
	};

};

RUM_AHel_LandHome = {
	private ["_mkrname","_grp","_wpNum","_pos","_wpAH","_ingress","_home","_distanceToIngress","_stg","_heloRTBWaypointH","_distanceToDeck","_heightToHomePlate","_distToHomePlate"];
	sleep 30;

	_mkrname = _this select 0;
	_grp = _this select 1;
	_home = _this select 2;
	_helo = _this select 3;
	_ingress = _this select 4;

	_wpAH =_grp addWaypoint [getpos _ingress, 0];
	_wpAH setWaypointType "MOVE";
	_wpAH setWaypointSpeed "NORMAL";
	_wpAH setWaypointCombatMode "BLUE";
	_wpAH setWaypointCompletionRadius 50;

	/* checks for final approach and whether the landing is on a LHD or ground */
	if (surfaceIsWater (getpos _home)) then {
		_heloRTBWaypoint = true;
		
		while {_heloRTBWaypoint} do {
			sleep .05;
			_distanceToIngress = round(_ingress distance _helo);
			hintsilent format["distance from home plate: %1",_distanceToIngress];
			if (_distanceToIngress <= 200) then {
				_wpAH = _grp addWaypoint [getposASL _home, 0];
				_wpAH setWaypointType "GETOUT";
				_wpAH setWaypointSpeed "LIMITED";
				_wpAH setWaypointBehaviour "CARELESS";
				_wpAH setWaypointCombatMode "BLUE";
				_wpAH setWaypointCompletionRadius 0;

				/* ends the while loop. */
				_heloRTBWaypoint = false;
			};
		};
		
		/* starts new while loop for landing procedures... */
		_heloRTBWaypointH = true;
		
		while {_heloRTBWaypointH} do {
			sleep 0.05;
			_distanceToDeck = round(getposASL _helo select 2);
			_heightToHomePlate = round(((getposASL _helo select 2)) - ((getposASL _home select 2)* 0 + RUM_AHEL_CarrierDeck));
			_distToHomePlate = round( _helo distance [(getposASL _home select 0),(getposASL _home select 1),((getposASL _home select 2)* 0 + RUM_AHEL_CarrierDeck)]);
			if (_distToHomePlate <= 70) then {
				if (_heightToHomePlate <= 5) then {
				sleep 3.5;
				_helo setFuel 0;
				sleep 5;

				/* ends the while loop. */
				_heloRTBWaypointH = false;
				}else{
					/* debug */
					hintsilent format ["Height Above Home Plate: %1\nDistance to Home Plate: %2",_heightToHomePlate,_distToHomePlate];
				};
			}else{
				/* debug */
				hintsilent format ["Height Above Home Plate: %1",_heightToHomePlate];
			};
		};
	} else {
		/* This section of the if statment is for land based helo's */
		
		/* waits till closer to ingress position before moving to final approach. */
		waituntil{
			sleep 0.15;
			((_ingress distance [getPos _helo select 0, getPos _helo select 1, 0]) <= 200);
		};

		_wpAH=_grp addWaypoint [getpos _home, 0];
		_wpAH setWaypointType "GETOUT";
		_wpAH setWaypointSpeed "NORMAL";
		_wpAH setWaypointBehaviour "CARELESS";
		_wpAH setWaypointCombatMode "BLUE";
		_wpAH setWaypointCompletionRadius 50;
		_helo flyInheight (RUM_AHEL_flyHeight - 45);
		_stg = "RUM_AHel_endflight = '"+(format ["%1",_helo])+"'";
		_wpAH setWaypointStatements ["true", _stg];
		
		/* end of flight check when helo has landed */
		_end = format ["%1",_helo];
		sleep 5;
		RUM_AHel_endflight = "";
		waitUntil{
			sleep 0.15;
			(!alive _helo) or (RUM_AHel_endflight == _end);
		};
	};
};
RUM_AHel_MoveFromStation = {
	private ["_mkrname","_grp","_pos","_wpAH","_helo"];
	_mkrname = _this select 0;
	_grp = _this select 1;
	_helo = _this select 2;
	_pos = [0,0,0];
	_pos = getMarkerpos _mkrname;
	_wpAH=_grp addWaypoint [_pos, 0];
	_wpAH setWaypointType "MOVE";
	_wpAH setWaypointSpeed "NORMAL";
	_wpAH setWaypointBehaviour "STEALTH";
	_wpAH setWaypointCombatMode "WHITE";
	_wpAH setWaypointCompletionRadius 50;
	_helo flyInheight RUM_AHEL_flyHeight;
};
RUM_AHel_MoveToStation = {
	private ["_mkrname","_grp","_pos","_wpAH","_helo","_heloRTBWaypointH","_stg"];
	_heloRTBWaypointH = true;
	_mkrname = _this select 0;
	_grp = _this select 1;
	_helo = _this select 2;
	_pos = [0,0,0];
	_pos = getMarkerpos _mkrname;
	_wpAH =_grp addWaypoint [_pos, 0];
	_wpAH setWaypointType "MOVE";
	_wpAH setWaypointSpeed "NORMAL";
	_wpAH setWaypointBehaviour "STEALTH";
	_wpAH setWaypointCombatMode "BLUE";
	_wpAH setWaypointCompletionRadius 0;
	_stg = "RUM_AHel_endflight = '"+(format ["%1",_helo])+"'";
	_wpAH setWaypointStatements ["true", _stg];
	
	/* waiting on waypoint complete/end mission variable */
	_end = format ["%1",_helo];
	RUM_AHel_endflight = "";
	waitUntil{
		sleep 0.15;
		(!alive _helo) OR (RUM_AHel_endflight == _end);
	};

};
RUM_AHel_moveWPStation = {
	private ["_mkrname","_grp","_pos","_wpAH","_helo","_markerpos","_helo_height","_heloRTBWaypointH","_stg","_helo_marker_distance","_heloNewStation"];
	player sidechat format ["%1 is moving to station.",_callsign];
	// station stage - helo will reach staitoned area and lower to a concealment height to settle until called upon
	_mkrname = _this select 0;
	_grp = _this select 1;
	_heloNewStation = _this select 2;
	_helo = _this select 3;
	_pos = [0,0,0];
	if(count _this == 4)then{_pos = getMarkerpos _mkrname}else{_pos = getpos (_this select 4)};
	_markerpos = getmarkerpos _mkrname;
	_helo_marker_distance = round (_markerpos distance [getPos _helo select 0, getPos _helo select 1, 0]);
	while {_heloNewStation} do {
		if ((_helo_marker_distance) <= 150) then{
			// this is a locality check for client side or server side
				_wpAH=_grp addWaypoint [_pos, 0];
				_wpAH setWaypointType "MOVE";
				_wpAH setWaypointSpeed "NORMAL";
				_wpAH setWaypointBehaviour "STEALTH";
				_wpAH setWaypointCombatMode "BLUE";
				_wpAH setWaypointCompletionRadius 0;
				_helo flyInheight RUM_AHEL_flyStationHeight;
				sleep 5;
				deleteWaypoint [_grp, 0];
				_heloNewStation = false;
			
		}else{
			// this will check to see if the helo has crashed or has been destroyed and exit the loop.
			If (!alive _helo) then {
				player sidechat format ["%1 has crashed or has been destroyed.", _helo];
				// hintsilent format ["%1 has crashed or has been destroyed.", _helo];
				_heloNewStation = false;
				exit;	
			}else{
				_helo_marker_distance = round (_markerpos distance [getPos _helo select 0, getPos _helo select 1, 0]);
				_helo_height = round (getPos _helo select 2);
				// hintsilent format ["Distance to Location: %1\n\nCurrent Altitude: %2", round _helo_marker_distance, _helo_height];
				sleep .05;
			};
		};
	};
};